﻿// -------------------------------------------------------------------------
//    @FileName         :    NFMiscPart.cpp
//    @Author           :    gaoyi
//    @Date             :    24-8-29
//    @Email            :    445267987@qq.com
//    @Module           :    NFMiscPart
//
// -------------------------------------------------------------------------

#include "NFMiscPart.h"

#include <NFEventDisp.h>

#include "proto_cs_login.pb.h"
#include "proto_db.pb.h"

#define PLAYER_ONLINESYN_TIME_GAP    60

NFMiscPart::NFMiscPart()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFMiscPart::~NFMiscPart()
{
}

int NFMiscPart::CreateInit()
{
    memset(m_sSecData, 0, sizeof(m_sSecData));
    m_dwLastOnlineSynTime = NF_ADJUST_TIMENOW();
    return 0;
}

int NFMiscPart::ResumeInit()
{
    return 0;
}

int NFMiscPart::LoadFromDB(const proto_ff::tbRoleDetail* pDBData)
{
    m_stData.FromPb(pDBData->role_other_data().misc_data());

    m_stData.secret_data.is_role_info = true;
    m_stData.power_burn_open = false;
    m_acquired_level_bouns.Set(pDBData->role_other_data().misc_data().acquired_player_level_bouns());
    return 0;
}

int NFMiscPart::InitConfig(proto_ff::tbRoleDetail* pRoleDetailOut, const RoleOne& stRoleOne)
{
    CHECK_NULL(0, pRoleDetailOut);
    auto pRoleMisc = pRoleDetailOut->mutable_role_other_data()->mutable_misc_data();
    CHECK_NULL(0, pRoleMisc);

    pRoleMisc->set_examiner_id(0);
    pRoleMisc->set_question_round(0);
    pRoleMisc->set_question_id(0);
    pRoleMisc->set_pre_question_state(proto_ff::EN_ANSWER_QUESTION);

    pRoleMisc->set_gacha_them(0);

    pRoleMisc->set_fp_tick_time(NF_ADJUST_TIMENOW());

    pRoleMisc->set_global_mail_time(0);
    pRoleMisc->mutable_secret_data()->set_is_add_friend(true);
    pRoleMisc->mutable_secret_data()->set_is_role_info(true);

    pRoleMisc->set_six_clock_update_time(NF_ADJUST_TIMENOW());

    auto pInitSetting = pRoleMisc->mutable_battle_drug_setting();
    CHECK_NULL(0, pInitSetting);

    pInitSetting->set_open(false);
    proto_ff::OneBattleDrugSetting* pOneSetting = pInitSetting->add_drug_settings();
    pOneSetting->set_battle_drug_type(proto_ff::E_Battle_Drug_Type_Hp);
    pOneSetting->set_id(0);
    pOneSetting->set_auto_use_percent(10);
    pOneSetting = pInitSetting->add_drug_settings();
    pOneSetting->set_battle_drug_type(proto_ff::E_Battle_Drug_Type_Mp);
    pOneSetting->set_id(0);
    pOneSetting->set_auto_use_percent(10);

    CHECK_EXPR(pInitSetting->drug_settings_size() == 2, -1, "unexpected setting size:{}", pInitSetting->drug_settings_size());
    return 0;
}

int NFMiscPart::SaveDB(proto_ff::tbRoleDetail* pDBData)
{
    m_stData.ToPb(pDBData->mutable_role_other_data()->mutable_misc_data());
    pDBData->mutable_role_other_data()->mutable_misc_data()->mutable_acquired_player_level_bouns()->clear();
    m_acquired_level_bouns.Get(*pDBData->mutable_role_other_data()->mutable_misc_data()->mutable_acquired_player_level_bouns());
    return 0;
}

void NFMiscPart::SetSecData(const char* data)
{
    if (data)
        strncpy(m_sSecData, data, 128);
    m_sSecData[128] = '\0';
}

int NFMiscPart::MakePlayerDataCS(proto_ff::Proto_CSGetRoleRsp* pCSGetRoleRsp)
{
    m_stData.ToPb(pCSGetRoleRsp->mutable_misc_data());
    pCSGetRoleRsp->mutable_misc_data()->mutable_acquired_player_level_bouns()->clear();
    m_acquired_level_bouns.Get(*pCSGetRoleRsp->mutable_misc_data()->mutable_acquired_player_level_bouns());
    return 0;
}

int NFMiscPart::MakeCityPlayerData(proto_ff::CityRoleInfo* pGCCityRoleInfo)
{
    return 0;
}

int NFMiscPart::SaveRoleSimpleInfo(proto_ff::Proto_RoleSimpleInfo* pDbData)
{
    m_stData.secret_data.ToPb(pDbData->mutable_secret_set());
    return 0;
}

int NFMiscPart::LogicTick()
{
    uint64_t now = NF_ADJUST_TIMENOW();
    if (now >= GetLastOnlineSynTime() + PLAYER_ONLINESYN_TIME_GAP)
    {
        NFEventDisp::OnPlayerOnlineTime(GetPlayer(), now - GetLastOnlineSynTime());
        SetLastOnlineSynTime(now);
        SetDailyOnlineTime(GetDailyOnlineTime() + PLAYER_ONLINESYN_TIME_GAP);
        MarkDirty();
    }
    return 0;
}

int NFMiscPart::OnLogin()
{
    SetLastOnlineSynTime(NF_ADJUST_TIMENOW());
    return 0;
}
